package com.ziq.zcamp.view.edit;

import static com.ziq.zcamp.util.CharUtils.*;

import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

public class SupportTopicsTextWatcher implements android.text.TextWatcher {

    private final static String TAG = "SupportTitleEditText";
    public final static String SYMBOL = "#";

    private final static char SPACE = ' ';
    private final static char LINEFEED = '\n';

    private boolean needSearch = false;

    private final TopicsSupportInterface mInterface;

    private int currentBrushInPosition = -1;

    private final boolean isDebug = true;

    public SupportTopicsTextWatcher(@NonNull TopicsSupportInterface supportInterface) {
        mInterface = supportInterface;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        needSearch = false;
    }

    //在变化时的新的字符串s里，从索引位置start开始，有count个字符，是替换了原来的before个字符的
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        CharSequence current = s.subSequence(start, start + count);

        currentBrushInPosition = mInterface.getSelectionStart() ;

        //这个是输入中含有 # 号的情况
        boolean lastIsSymbol = !TextUtils.isEmpty(current) && SYMBOL.equals(current.subSequence(current.length() - 1, current.length()).toString());
        if (lastIsSymbol) {
            mInterface.apply();
            needSearch = true;
        }

        //产品说这为null给搜索显示我们的默认话题，所以这里触发一下
        //这个是输入中含有没有 # 号的情况,但是前面刚好有个#号的情况，删除的时候出现
        boolean startFrontIsSymbol = s.subSequence(Math.max(start-1,0),start).toString().equals(SYMBOL);
        if (startFrontIsSymbol || current.toString().equals(SYMBOL)){
            needSearch = true;
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        //这里进行截断的操作
        truncation(s);
        //变色开始
        mInterface.drawTopicsColor(s);
        //这里进行搜索的回调
        mInterface.handlerSearch(needSearch);
        mInterface.inputProcessingCompleted();
    }

    private void truncation(Editable s) {
        //当前处理的字符串对于整个字符串的偏移量
        int offset = 0;
        //处理所有字符序列
        CharSequence handleCharSequence = s.subSequence(offset, s.length());
        int findSymbol = -1;

        mInterface.clearTopics();
        for (int i = 0; i < handleCharSequence.length(); i++) {
            int current = Character.codePointAt(handleCharSequence, i);
            if (isDebug){
                Log.i("char", "下标i=" + i +" 当前的char为【" + (char) current + "】编码为【" + current + "】");
            }
            if (current == SYMBOL.charAt(0)) {
                //判断前一个话题截断没
                if (findSymbol != -1) {
                    //截断当前话题
                    s.insert(i, SPACE + "");
                    //中断当前的循环，等待下次递归调用此方法，不中断会导致维护的topics数据错误
                    return;
                }

                findSymbol = i;
                mInterface.removeTopics(offset + findSymbol);
            }

            if (findSymbol != -1 && i > findSymbol) {
                if (current == SPACE) {
                    //截断当前话题
                    findSymbol = -1;
                } else if (current == LINEFEED) {
                    //截断当前话题
                    s.insert(i, SPACE + "");
                    return;
                } else if (i - findSymbol > 30) {
                    //截断当前话题
                    s.insert(i, SPACE + "");
                    return;
                } else if (isEmojiCharacter(current)) {
                    //截断当前话题
                    s.insert(i, SPACE + "");
                    return;
                }
                //输入除英文字母、汉字、阿拉伯数字之外的内容
                else if (!(isChinese(current) || isNum(current) || isLetter(current))) {
                    //截断当前话题
                    s.insert(i, SPACE + "");
                    return;

                } else {
                    boolean isMore = mInterface.addTopics(offset + findSymbol, s.subSequence(findSymbol, Math.min(i + 1, s.length())).toString());
                    if (isMore)break;
                    if (currentBrushInPosition>= findSymbol && currentBrushInPosition<=Math.min(i + 1, s.length())){
                        needSearch = true;
                    }
                }
            }
        }

    }

}